************************************************************
* Figure 1: Average Turnout in Referendums by  Postal Voting
* Before and After the Introduction of Postal Voting, 1981-2009 
* (Canton-level Referendum Data)
************************************************************

	scalar year_up=2011

	use data1.dta, clear
	
	* (i) Generate indicator when a specific cantons changes to postal voting (postal_change_timer)
	
	g postal_all_diff=D1.postal_all	
	g help1=abstag if postal_all_diff==1
	bysort knr: egen postal_change_timer=min(help1)
	
	
	* (ii) Regress turnout on a FE for the referendum day and take residual
	
	reg bet_mean i.abstag
	predict double resid, residuals
	
	
	* (iii) Reset time variable for each canton (t_0=0 equals the time period of postal voting
	
	gen ones=1
	
	capture mkdir "partrends"
	
	forvalues cant=1(1)26{
	preserve
	keep if canton==`cant' & abstag==1
	rename postal_change_timer postal_change_timer_`cant'
	keep postal_change_timer_`cant'	ones
	saveold ./partrends/temp.dta, replace version(12)
	restore
	
	preserve
	merge m:1 ones using ./partrends/temp.dta, gen(merge_cantons)
	gen abstag_rel_canton=abstag-postal_change_timer_`cant' // reset time variable
	
	bysort abstag_rel_canton: egen turnout_treatmentgroup=mean(bet_mean) if canton==`cant'
	bysort abstag_rel_canton: egen residuals_treatmentgroup=min(resid) if canton==`cant'

	bysort abstag_rel_canton: egen help2=mean(bet_mean) if postal_all==0 & canton!=`cant' // take only those cantons that did not practice postal voting
	bysort abstag_rel_canton: egen turnout_controlgroup=mean(help2) 
	bysort abstag_rel_canton: egen help3=mean(resid) if postal_all==0 & canton!=`cant' // take only those cantons that did not practice postal voting
	bysort abstag_rel_canton: egen residuals_controlgroup=min(help3) 
	
	keep canton kkurz abstag_rel_canton  turnout_treatmentgroup turnout_controlgroup residuals_treatmentgroup residuals_controlgroup datum
	keep if canton==`cant'
	gen time_var1=floor( abstag_rel_canton/5)
	gen time_var2=floor( abstag_rel_canton/10)
	saveold ./partrends/parallel_trends_`cant'.dta, version(12) replace
	restore
	}
	
	
	* (iv) Append all individual files
	
	use ./partrends/parallel_trends_1.dta,  clear
	
	forvalues cant=2(1)26{
	append using ./partrends/parallel_trends_`cant'.dta
	erase ./partrends/parallel_trends_`cant'.dta
	}
	
	erase ./partrends/parallel_trends_1.dta
 	erase ./partrends/temp.dta

	
	drop kkurz
	rename canton knr
	
	merge m:1 knr using data2.dta, gen(cant_transl)

	
	
	saveold ./partrends/parallel_trends_all.dta, version(12) replace

	
	
	
